package thu.cs.segroup;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import jade.core.Runtime;
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.wrapper.*;

public class RemoteExec {

public static void main(String args[]) {
		
		try {
			
			Runtime rt = Runtime.instance();
			rt.setCloseVM(true);
			ProfileImpl pContainer = new ProfileImpl(null, 1800, null);
			pContainer.setParameter("host", "166.111.134.182");
			String localIP = RemoteExec.getLocalIPAddress();
			pContainer.setParameter("container-name", localIP);
			AgentContainer cont = rt.createAgentContainer(pContainer);
			AgentController rma = cont.createNewAgent("rma@"+localIP, "jade.tools.rma.rma", new Object[0]);
			rma.start();
						
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String getLocalIPAddress() {
		Enumeration<NetworkInterface> netInterfaces = null;   
		try {   
			netInterfaces = NetworkInterface.getNetworkInterfaces();   
			while (netInterfaces.hasMoreElements()) {   
				NetworkInterface ni = netInterfaces.nextElement();   
				// System.out.println("DisplayName:" + ni.getDisplayName());   
				// System.out.println("Name:" + ni.getName());   
				if (ni.getName().equalsIgnoreCase("eth0")) {
					Enumeration<InetAddress> ips = ni.getInetAddresses();
					while (ips.hasMoreElements()) {   
						// System.out.println("IP:"  
						// + ips.nextElement().getHostAddress());
						String ip = ips.nextElement().getHostAddress();
						if (ip.indexOf(".") != -1) {
							return ip;
						}
					}
				}	     
			}   
		} catch (Exception e) {   
			e.printStackTrace();   
		} 
		return null;
	}
	
}
